package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.UserThreadLocal;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-02
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

//    /**
//     * 分页查询预约
//     *
//     * @param reservation 预约信息
//     * @return 预约信息
//     */
//    @Override
//    public List<Reservation> selectReservationList(Reservation reservation)
//    {
//        return reservationMapper.selectReservationList(reservation);
//    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
//      设置默认值
        reservation.setCreateBy(SecurityUtils.getUsername());
        reservation.setType(0);
         reservation.setStatus(0);
//         检查同一天同一时间段是否已存在预约
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("time", reservation.getTime())
                     .eq("status", 0)
                     .eq("mobile", reservation.getMobile());
      if (count(queryWrapper) > 0){
          throw new BaseException("同一时间段已存在预约");
      }
      return save
              (reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
//    @Override
//    public int updateReservation(Reservation reservation)
//    {
//        return updateById(reservation) ? 1 : 0;
//    }
    /**
     * 2.1 查询当天取消预约数量
     *
     * @param
     * @return 结果
     */
    @Override
    public int getCancelledCountForToday() {
        // 获取当前用户ID
        long userId = UserThreadLocal.getUserId();
        // 计算当天开始和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 查询当天取消预约数量
        Integer count = reservationMapper.countCancelledReservations(userId, startTime, endTime);
        return count;

    }
//    @Override
//    public Integer countCancelledReservations(Long userId, LocalDateTime startTime, LocalDateTime endTime) {
//        return reservationMapper.countCancelledReservations(userId, startTime, endTime);
//    }
    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time 预约时间
     * @return 结果
     */
    @Override
    public List<ReservationVo> countByTime(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1).minusNanos(1);
        List<ReservationVo> list = reservationMapper.countByTime(startTime, endTime);
        return list;
    }

    /**
     * 取消预约
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int cancelReservation(Long id) {
//  检查预约是否存在
        Reservation reservation = selectReservationById(id);
        if (reservation == null) {
            throw new BaseException("预约不存在");
        }
//        检查预约状态
        if (reservation.getStatus()== 2) {
           throw new BaseException("预约已取消");
        }
        // 更新预约状态为已取消
        Reservation  updateReservation = new Reservation();
        updateReservation.setId(id);
        updateReservation.setStatus(2);
        return updateById(updateReservation) ? 1 : 0;
    }



}
